﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Data.OleDb;
using System.Data;

namespace dsjsports
{
    /// <summary>
    /// Логика взаимодействия для SportsmenList.xaml
    /// </summary>
    public partial class SportsmenList : UserControl
    {
        public SportsmenList()
        {
            InitializeComponent();
            viewSportsmens.Filter = TextFilter;
        }

        private Synchronizer synchronizer;
        public void SynchronizerSet(Synchronizer Synchronizer)
        {
            this.synchronizer = Synchronizer;
        }

        public void Clear()
        {
            sportsmens.Clear();
        }

        private static ObservableCollection<Sportsmen> sportsmens = new ObservableCollection<Sportsmen>();
        
        private ICollectionView viewSportsmens = CollectionViewSource.GetDefaultView(sportsmens);
        public ICollectionView Sportsmens
        {
            get { return viewSportsmens; }

        }

        public void Add(Sportsmen newSportsmen)
        {
            newSportsmen.SportsmenChanged += new Sportsmen.SportsmenChangedEventHandler(SportsmenChang);
            sportsmens.Add(newSportsmen);
        }

        public void Remove(Sportsmen oldSportsmen)
        {
            sportsmens.Remove(oldSportsmen);
        }
        
        //мментальная синхронизация
        private void SportsmenChang(object sender, SportsmenChangedEventArgs a)
        {
            synchronizer.ChangeSportsmen(((Sportsmen)sender), a.ModifiedField, a.NewValue.ToString(), ((Sportsmen)sender).ID.ToString());  
        }
        
        //добавление
        private void ADD_Click(object sender, RoutedEventArgs e)
        {
            //да я временами индус!
            List<int> IdS = new List<int>();
            foreach (Sportsmen s in sportsmens) 
                IdS.Add(s.ID);
            IdS.Sort();
            int id = 0;
            foreach (int i in IdS)
                if (i == id) 
                    id++;
            //индусить надо в меру

            Sportsmen newSportsmen = new Sportsmen(id, "имя", "отчество", "фамилия", SportsRank.бр, DateTime.Now, Floor.М, "город", "клуб");
            newSportsmen.SportsmenChanged += new Sportsmen.SportsmenChangedEventHandler(SportsmenChang);
            sportsmens.Add(newSportsmen);
            synchronizer.AdditionSportsmen(newSportsmen);
            sportsmenDataGrid.SelectedItem = newSportsmen;
        }
        //удаление
        private void Delite_Click(object sender, RoutedEventArgs e)
        {
            Sportsmen delSportsmen = (sportsmenDataGrid.SelectedItem as Sportsmen);
            synchronizer.RemovalSportsmen(delSportsmen);
            synchronizer.RemovalSportsmen(delSportsmen);
            sportsmens.Remove(delSportsmen);
        }

       
        public bool TextFilter(object o)
        {
            Sportsmen p = (o as Sportsmen);
            if (p == null)
                return false;

            if (p.LastName.ToLower().Contains(SearchTextBox.Text.ToLower()) ||
                p.Name.ToLower().Contains(SearchTextBox.Text.ToLower()) ||
                p.MiddleName.ToLower().Contains(SearchTextBox.Text.ToLower()) ||
                p.Clab.ToLower().Contains(SearchTextBox.Text.ToLower()) ||
                p.Sity.ToLower().Contains(SearchTextBox.Text.ToLower()))
                return true;
            else
                return false;
        }

        private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            viewSportsmens.Refresh();
        }
    }

    public class WidthConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ((double)value) - 25;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ((double)value) + 25;
        }
    }
    public class ActualConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (((bool)value)) return Colors.Green;
            else return Colors.Pink;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (((Color)value) == Colors.Green) return true;
            else return false;
        }
    }
    public class NotActualConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (((bool)value)) return Visibility.Collapsed;
            else return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (((Visibility)value) == Visibility.Collapsed) return true;
            else return false;
        }
    }
}